<?php
namespace Agent\Controller;

use Utuo\BackgroundController;
use My\Model\OrderModel;

class AgentSaleStatisticsController extends BackgroundController {
    
    protected $myselfSales = array(
        '0' => 0.35,
        '1' => 0.13,
        '3' => 1,
        '4' => 1,
        '5' => 1,
        '6' => 1,
        '7' => 1,
    );
    
    protected $downSales = array(
        '0' => 0.12,
        '1' => 0.35,
        '3' => 1,
        '4' => 1,
        '5' => 1,
        '6' => 1,
        '7' => 1,
    );
    
    public function _initialize() {
        $this->setControllerTitle("代理销售统计");
        $this->setActionTitle(array(
            'index' => '销售统计',
        ));
        $this->setActionNav('index');
    }
    
    /**
     * 销售统计(代理关系按推荐关系非层级关系)
     * @see \Utuo\BackgroundController::indexAction()
     */
    public function indexAction() {
        $startDate = I('startDate', 0, 'intval') or $startDate = strtotime('-29 days');
        $endDate = I('endDate', 0, 'intval') or $endDate = time();
        $params = array(
            'startDate' => $startDate,
            'endDate' => $endDate,
            'agentId' => $this->aid,
        );
        $agent = M("Agent")->where("id=%d", $this->aid)->find();
        $this->assign("agent", $agent);
        $result =  $this->search($params);
        $this->assign($result);
        $this->assign("params", $params);
        $this->assign("myselfSales", $this->myselfSales);
        $this->assign("downSales", $this->downSales);
        $this->display();
    }
    
    /**
     * 统计查询
     * @param unknown $params
     */
    public function search( $params ) {
        $m = M("DistributionShopOrder");
        $condition = array(
            'create_time' => array('between', array($params['startDate'], $params['endDate'])),
            'agent_id' => $params['agentId'],
            'pay_status' => OrderModel::PAY_STATUS_PAID,
        );
        /* 下线订单 */
        $downSaleList = null;
        /* 我的订单 */
        $mySale = $m->where($condition)->sum('actual_cost'); // 我的销售额
        
        /* 我推荐的人的销售额 */
        // 我推荐的人
        $downAgentIds = M("Agent")->where(array('tree'=>array("exp", "is not null and find_in_set('{$this->aid}', tree)")))->getField("id", true);
        if ( $downAgentIds ) {
            $condition = array(
                'a.create_time' => array('between', array($params['startDate'], $params['endDate'])),
                'a.pay_status' => OrderModel::PAY_STATUS_PAID,
                'a.agent_id' => array('in', $downAgentIds),
                '_string' => "a.agent_id!={$this->aid}",
            );
            $m0 = M("DistributionShopOrder a")->join("__AGENT__ b on a.agent_id=b.id")
                                              ->where($condition)
                                              ->group("a.agent_id")
                                              ->field("sum(a.actual_cost) as sum,b.*")
                                              ->order("level asc");

            $downSaleList = $m0->select();
        }
        return array("mySale" => $mySale, "downSaleList" => $downSaleList);
    }
    
}